﻿using SwitchHostProject.Command;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using SwitchHostProject.View;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows;
using SwitchHostProject.Model;
using System.Collections.ObjectModel;
using System.Collections;
using System.Windows.Media;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;

namespace SwitchHostProject.ViewModel
{
    public partial class MainViewModel : ObservableObject
    {

        public RichTextBox hostContentRichTextBox;

        public ObservableCollection<RowItem> RowItemList { get; set; }

        public ObservableCollection<HostGroup> HostGroups { get; set; } = new ObservableCollection<HostGroup>();

        public MainViewModel(RichTextBox richTextBox)
        {
            hostContentRichTextBox = richTextBox;

            RowItemList = new ObservableCollection<RowItem>();

            init();
        }

        private void init()
        {
            HostGroup hg1 = new HostGroup("激活", "1", true);
            HostGroup hg0 = new HostGroup("非激活", "1", false);
            HostGroups.Add(hg0);
            HostGroups.Add(hg1);

            HostGroup hgp1 = new HostGroup("分组1", "0", [hg1, hg0, hg1]);
            HostGroups.Add(hgp1);

            HostGroup hgp2 = new HostGroup("分组2", "0", [hg1, hg0, hg1]);
            HostGroups.Add(hgp2);


        }

        [RelayCommand]
        private void OpenWindow()
        {

        }

        /**
        * 展示主题host映射内容
        */
        [RelayCommand]
        public void ClientHost(object parameter)
        {
            string hostFilePath = @"C:\Windows\System32\drivers\etc\hosts";
            // 创建一个新的 FlowDocument（文档对象）
            FlowDocument newDocument = new FlowDocument();
            try
            {
                string[] lines = File.ReadAllLines(hostFilePath);
                int i = 1;
                foreach (var line in lines)
                {
                    Paragraph paragraph = new Paragraph();
                    paragraph.LineHeight = 1.5;
                    Run run = new Run(line);
                    run.FontSize = 16;
                    paragraph.Inlines.Add(run);
                    if (line.StartsWith("#"))
                    {
                        Color color = Color.FromRgb(77, 166, 74);
                        paragraph.Foreground = new SolidColorBrush(color);
                    }
                    // 将段落添加到文档中
                    newDocument.Blocks.Add(paragraph);
                    RowItemList.Add(new RowItem(i++));
                }
                // 设置 RichTextBox 的 Document 为新创建的文档
                hostContentRichTextBox.Document = newDocument;
                hostContentRichTextBox.IsReadOnly = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "发生错误", MessageBoxButton.OKCancel);
            }
        }
    }
}
